var form
var table
var G_type
var G_id
$(document).ready(function () {
  initLayui()
})

function initLayui() {
  layui.use(function () {
    form = layui.form;
    table = layui.table;

    // 保存人员
    form.on('submit(add-form)', function (data) {
      var params = {
        id: G_type == '1' ? null : G_id,
        name: data.field.name,
        account: data.field.account,
        authId: data.field.authId,
      }
      AjaxRequest('/user/save', callBack, params, 'post')

      function callBack(result) {
        if (result.status == 200) {
          layer.closeAll()
          layer.msg('保存成功')
          $('#search-btn').click()
        } else {
          layer.msg('保存失败')
        }
      }

      return false;
    })

    //查询
    form.on('submit(search)', function (data) {
      var params = {
        name: data.field.name,
      }
      initTable(params)
      return false;
    });

    $('#search-btn').click()
  })
}

// 新建用户弹窗
function showAddWindow(type, res) { //type:1新增 2编辑
  G_type = type
  layer.open({
    type: 1,
    shadeClose: false,
    move: false,
    closeBtn: 2,
    skin: 'default-layer',
    area: ['auto', '4.7rem'],
    title: type == '1' ? '新增' : '编辑',
    content: `<div id="add-from" class="layui-form" lay-filter="saveForm" style="padding: .2rem 0.6rem;color:#E4EFFF">
                <div class="layui-form-item">
                  <div class="layui-inline">
                    <div>用户名称：</div>
                    <div style="margin-top: .1rem;">
                      <input type="text" name="name" lay-verify="required" class="layui-input">
                    </div>
                  </div>
                </div>
                <div class="layui-form-item">
                  <div class="layui-inline">
                    <div>登录账号：</div>
                    <div style="margin-top: .1rem;">
                      <input type="text" name="account" lay-verify="required" class="layui-input">
                    </div>
                  </div>
                </div>
                <div class="layui-form-item">
                  <div class="layui-inline">
                    <div>用户角色：</div>
                    <div style="margin-top: .1rem;">
                      <select name="authId" lay-verify="required">
                        <option value="">请选择</option>
                        <option value="1">管理员</option>
                        <option value="2">普通用户</option>
                      </select>
                    </div>
                  </div>
                </div>
                <div class="layui-form-item">
                  <div class="layui-inline">
                    <div>
                      <i class="layui-icon layui-icon-tips-fill"></i>
                      <span>默认密码123456,登录后点击“我的”可修改</span>
                    </div>
                  </div>
                </div>
                <div class="layui-form-item" style="text-align:center">
                  <button type="submit" class="layui-btn layui-btn-lg layui-bg-blue" lay-submit lay-filter="add-form" style="border-radius:.08rem">保  存</button>
                </div>
              </div>`,
    success: function (layero, index) {
      form.render()
      if (type == '2') {
        G_id = res.id
        //给表单赋值
        form.val("saveForm", {
          "name": res.name,
          "account": res.account,
          "authId": res.authId,
        });
      }

    }
  });
}

function initTable(params) {
  layui.use('table', function () {
    var height = $('.right-block').height() - 90

    //第一个实例
    table.render({
      title: '',
      height: height,
      text: {
        none: '暂无数据'
      },
      elem: '#table',
      skin: 'nob',
      even: true,
      size: 'lg',
      headers: {
        SessionId: JSON.parse(localStorage.getItem('userInfo')).sessionId
      },
      url: ajaxPrefix() + '/user/list',
      where: {
        name: params.name,
      },
      parseData: function (res) { //res 即为原始返回的数据
        return {
          "code": res.status == 200 ? 0 : 1, //解析接口状态
          "msg": res.message, //解析提示文本
          "count": res.result.total, //解析数据长度
          "data": res.result.data //解析数据列表
        };
      },
      request: {
        pageName: 'pageNum', //页码的参数名称，默认：page
        limitName: 'pageSize' //每页数据量的参数名，默认：limit
      },
      page: true,
      limits: [10, 20, 30, 40, 50, 60, 70, 80, 90],
      cols: [
        [ //表头
          {
            field: 'name',
            title: '用户名称',
            align: 'center'
          },
          {
            field: 'account',
            title: '登录账号',
            align: 'center'
          },
          {
            field: '',
            title: '用户角色',
            align: 'center',
            templet: function (d) {
              return d.authId == 1 ? '管理员' : '普通用户'
            }
          },
          {
            field: 'createTime',
            title: '创建时间',
            align: 'center'
          },
          {
            field: 'updateTime',
            title: '更新时间',
            align: 'center'
          },
          {
            field: '',
            title: '操作',
            align: 'center',
            width: 180,
            templet: function (d) {
              let res = JSON.stringify(d);
              return `<div class="operate-col">
                        <div class="operate-item" onclick='showAddWindow("2",${res})'>
                          <i class="layui-icon layui-icon-edit"></i>
                          编辑
                        </div>
                        <div class="operate-item" onclick='showDelWindow(${res})'>
                          <i class="layui-icon layui-icon-delete"></i>
                          删除
                        </div>
                      </div>`
            },
          },
        ]
      ]
    });

  });
}

// 删除
function showDelWindow(res) {
  layer.confirm('确认是否删除?', {
    btn: ['确认', '取消'] //按钮
  }, function () {
    AjaxRequest('/user/delete?id=' + res.id, callBack)

    function callBack(result) {
      if (result.status == 200) {
        layer.msg('删除成功')
        $('#search-btn').click()
      } else {
        layer.msg('删除失败')
      }
    }
  });
}